package org.itsolutions.javaBackup.buffers;

import java.nio.Buffer;
import java.nio.ByteBuffer;

/**
 * 
 * @author Tomas Fecko
 */
public final class ByteBufferWrapper {
	
	private final ByteBuffer		mByteBuffer;
	private final ByteBufferPool	mPool;
	private final boolean			mIsFromPool;
	
	/**
	 * @param pByteBuffer byte buffer
	 */
	public ByteBufferWrapper(ByteBuffer pByteBuffer) {
		mByteBuffer = pByteBuffer;
		mIsFromPool = false;
		mPool = null;
	}
	
	/**
	 * @param pByteBuffer byte buffer
	 * @param pPool pool
	 * @param pIsFromPool if the byteBufferWrapper is from pool
	 */
	protected ByteBufferWrapper(ByteBuffer pByteBuffer, ByteBufferPool pPool, boolean pIsFromPool) {
		mByteBuffer = pByteBuffer;
		mPool = pPool;
		mIsFromPool = pIsFromPool;
	}

	/**
	 * @return the isFromPool
	 */
	public boolean isFromPool() {
		return mIsFromPool;
	}

	/**
	 * 
	 * @return true if release was successfull
	 */
	public boolean releaseByteBuffer() {
		if (!mIsFromPool) {
			return false;
		}
		clear();
		if (mPool != null) {
			return mPool.returnByteBuffer(this);
		} else {
			return false;
		}
	}

	/**
	 * @return buffer
	 * @see java.nio.Buffer#clear()
	 */
	public Buffer clear() {
		return mByteBuffer.clear();
	}

	/**
	 * @return true if has remaining
	 * @see java.nio.Buffer#hasRemaining()
	 */
	public boolean hasRemaining() {
		return mByteBuffer.hasRemaining();
	}

	/**
	 * @return the byteBuffer
	 */
	public ByteBuffer getByteBuffer() {
		return mByteBuffer;
	}


	/**
	 * @see java.nio.Buffer#flip()
	 * @return buffer
	 */
	public Buffer flip() {
		return mByteBuffer.flip();
	}

	/**
	 * @return capacity
	 * @see java.nio.Buffer#capacity()
	 */
	public int capacity() {
		return mByteBuffer.capacity();
	}

	/**
	 * @return posistion
	 * @see java.nio.Buffer#position()
	 */
	public int position() {
		return mByteBuffer.position();
	}

	/**
	 * @return limit
	 * @see java.nio.Buffer#limit()
	 */
	public int limit() {
		return mByteBuffer.limit();
	}

	/**
	 * @param pNewLimit new limit
	 * @return buffer
	 * @see java.nio.Buffer#limit(int)
	 */
	public Buffer limit(int pNewLimit) {
		return mByteBuffer.limit(pNewLimit);
	}

	/**
	 * @return buffer
	 * @see java.nio.Buffer#rewind()
	 */
	public Buffer rewind() {
		return mByteBuffer.rewind();
	}

	/**
	 * @return remaining
	 * @see java.nio.Buffer#remaining()
	 */
	public int remaining() {
		return mByteBuffer.remaining();
	}
}







